package entities.profiles;

import entities.User;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "passenger_profile")
@NamedQueries({
    @NamedQuery(name = "PassengerProfile.findAll", query = "SELECT p FROM PassengerProfile p"),
    @NamedQuery(name = "PassengerProfile.findByIdPassengerProfile", query = "SELECT p FROM PassengerProfile p WHERE p.idPassengerProfile = :idPassengerProfile"),
    @NamedQuery(name = "PassengerProfile.findByEvaluationsTotal", query = "SELECT p FROM PassengerProfile p WHERE p.evaluationsTotal = :evaluationsTotal"),
    @NamedQuery(name = "PassengerProfile.findByGoodHumored", query = "SELECT p FROM PassengerProfile p WHERE p.goodHumored = :goodHumored"),
    @NamedQuery(name = "PassengerProfile.findByPunctuality", query = "SELECT p FROM PassengerProfile p WHERE p.punctuality = :punctuality")})
public class PassengerProfile implements Serializable {
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "idPassengerProfile")
    private User user;
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idPassengerProfile")
    private Integer idPassengerProfile;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "evaluationsTotal")
    private long evaluationsTotal;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "goodHumored")
    private long goodHumored;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "punctuality")
    private long punctuality;
    
    /*@OneToOne(cascade = CascadeType.ALL, mappedBy = "idPassengerProfile")
    private User user;*/

    public PassengerProfile() {
        this.evaluationsTotal = 0;
        this.goodHumored = 0;
        this.punctuality = 0;
    }

    /*public PassengerProfile(User user) {
        this.user = user;
        this.evaluationsTotal = 0;
        this.goodHumored = 0;
        this.punctuality = 0;
    }*/

    public Integer getId() {
        return idPassengerProfile;
    }
    
    public void evaluate(int goodHumored, int punctuality){
        if(goodHumored > 10) {
            goodHumored = 10;
        }
        if(punctuality > 10) {
            punctuality = 10;
        }
        if(goodHumored < 1) {
            goodHumored = 1;
        }
        if(punctuality < 1) {
            punctuality = 1;
        }
        ++evaluationsTotal;
        this.goodHumored += goodHumored;
        this.punctuality += punctuality;
    }

    public long getEvaluationsTotal() {
        return evaluationsTotal;
    }

    public long getGoodHumored() {
        return goodHumored;
    }

    public long getPunctuality() {
        return punctuality;
    }
    
    public long getGoodHumoredAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return goodHumored/evaluationsTotal;
    }

    public long getPunctualityAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return punctuality/evaluationsTotal;
    }

    /*public User getUser() {
        return user;
    }*/

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idPassengerProfile != null ? idPassengerProfile.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (!(object instanceof PassengerProfile)) {
            return false;
        }
        PassengerProfile other = (PassengerProfile) object;
        return !((this.idPassengerProfile == null && other.idPassengerProfile != null) || (this.idPassengerProfile != null && !this.idPassengerProfile.equals(other.idPassengerProfile)));
    }

    @Override
    public String toString() {
        return "entities.profiles.PassengerProfile[ idPassengerProfile=" + idPassengerProfile + " ]";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
